package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.annotation.NeedLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户声音播放进度管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({ "all" })
public class UserListenProcessApiController {

    @Autowired
    private UserListenProcessService userListenProcessService;

    /**
     * 更新用户声音播放进度
     *
     * @param userListenProcessVo 用户播放进度信息
     * @return 结果
     */
    @PostMapping("/userListenProcess/updateListenProcess")
    @Operation(summary = "更新用户声音播放进度")
    @NeedLogin
    public Result<?> updateListenProcess(@Valid @RequestBody UserListenProcessVo userListenProcessVo) {
        // 从 ThreadLocal 中获取用户ID
        Long userId = AuthContextHolder.getUserId();
        userListenProcessService.updateListenProcess(userId, userListenProcessVo);
        return Result.ok();
    }

    /**
     * 获取声音的上次播放进度
     *
     * @param trackId 声音ID
     * @return 播放进度（秒）
     */
    @GetMapping("/userListenProcess/getTrackBreakSecond/{trackId}")
    @Operation(summary = "获取声音的上次播放进度")
    @NeedLogin
    public Result<BigDecimal> getTrackBreakSecond(@PathVariable Long trackId) {
        // 从 ThreadLocal 中获取用户ID
        Long userId = AuthContextHolder.getUserId();
        BigDecimal breakSecond = userListenProcessService.getTrackBreakSecond(userId, trackId);
        return Result.ok(breakSecond);
    }

}
